/**
 * Created by billy on 2017/4/25.
 */
var g = require("nodeLib");
var resultObj = require("../work");
// var classObj = require("../type");
// var lasterItemObj = {};
var _sql,_sqlBase;

var _file = g.data.file.get("user");
module.exports = class {
	constructor()
	{
		_file.add(__projpath("./module/sql/user"));
		_sql = g.data.manager.getManager("mysql");
		_sqlBase = g.data.manager.getManager("bluewoniu");
		this.add("run", this.run);//导入sql表数据
// 		this.add("test2", this.test2);
// 		this.add("hello", this.hello);
	}

	run($data, $succcess, $error, $client)
	{

		var sqlStr = _file.get("selectData.sql");
		_sql.query(sqlStr, ($list) => {
			var _sqlStr = "";
			var id = 0;
// 			trace($list);
			for (var key in resultObj)
			{
				for (var i = 0; i < resultObj[key].length; i++)
				{
					var optionsType, formId, class1, class2;
					id++;
					for (var j = 0; j < $list.length; j++)
					{

						if (key == 'mood')
						{
							optionsType = 1;
						}
						else
						{
							optionsType = 0;
						}
						formId = getNum(key);
						var keys = resultObj[key][i];
						if (resultObj[key][i].class2)
						{
							if ($list[j].name == resultObj[key][i].class2)
							{
								class2 = $list[j].id;
								class1 = $list[j].parentId;
							}
						}
						else
						{
							if ($list[j].name == resultObj[key][i].class1)
							{
								class1 = $list[j].id;
								class2 = class1;
							}
						}
					}
					;
					_sqlStr += _file.get("insertFormData.sql",
						{
							id: id,
							formId: formId,
							class1: class1,
							class2: class2,
							title: keys.title,
							tools: keys.tools,
							way: keys.way,
							target: keys.target,
							type:keys.type,
							options: keys.options,
							optionsType: optionsType,
							minAge: keys.minAge,
							maxAge: keys.maxAge
						}
					);
					trace(_sqlStr);
				}
			}
			_sqlBase.query(_sqlStr,($list)=>{
				trace("DONE~~~~~")
				process.exit();
			})

		})
	}
}
function getNum($key)
{
	var formId;

	if ($key == 'action')
	{
		formId = 2;
	}
	else if ($key == 'smallAction')
	{
		formId = 3
	}
	else if ($key == 'know')
	{
		formId = 5
	}
	else if ($key == 'contact')
	{
		formId = 6
	}
	else if ($key == 'language')
	{
		formId = 4
	}
	else if ($key == 'mood')
	{
		formId = 8;

	}
	else if ($key == 'self')
	{
		formId = 7
	}
	else if ($key == 'result')
	{
		formId = 1
	}
	return formId;
}